package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.queryObject.StrategyQueryObject;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.service.IStrategyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class StrategyServiceImpl implements IStrategyService {

    @Autowired
    private StrategyRepository strategyRepository;
    @Autowired
    private MongoTemplate template;

    @Override
    public void save(Strategy strategy) {
        // 以防万一把要保存的对象的id设置为空
        strategy.setId(null);
        strategyRepository.save(strategy);
    }

    @Override
    public void update(Strategy strategy) {
        strategyRepository.save(strategy);
    }

    @Override
    public void delete(String id) {
        strategyRepository.deleteById(id);
    }

    @Override
    public Strategy get(String id) {
        Optional<Strategy> strategy = strategyRepository.findById(id);
        return strategy.orElse(null);
    }

    @Override
    public List<Strategy> list() {
        List<Strategy> all = strategyRepository.findAll();
        return all;
    }

    @Override
    public Page<Strategy> query(StrategyQueryObject qo) {

        // 封装查询条件
        Query query = new Query();

        // 拼接查询条件
        if (StringUtils.hasLength(qo.getThemeId())){
            query.addCriteria(Criteria.where("themeId").is(qo.getThemeId()));
        }
        if (StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }

        // 总条数
        long totalCount = template.count(query, Strategy.class);

        // 如果总条数为0 , 那么返回空的分页对象
        if (totalCount==0){
            return Page.empty();
        }

        // 分页对象 , 参数1为当前页 从0开始 , 参数2为每页显示条数 , 参数3为排序方式, 参数4为按哪个字段排序
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(),
                Sort.Direction.ASC,"_id");

        query.with(pageable);// 添加查询条件

        List<Strategy> list = template.find(query, Strategy.class);

        // PageImpl 为 Page 接口的一个实现类
        return new PageImpl<>(list, pageable,totalCount );
    }

    @Override
    public List<Destination> queryStrategiesByDestId(String id) {

        List<Destination> list = strategyRepository.findByDestId(id);

        return list;
    }

    @Override
    public List<Strategy> queryStrategiesByCatalogId(String id) {

        List<Strategy> list = strategyRepository.findByCatalogId(id);

        return list;
    }

    @Override
    public List<Strategy> queryViewnumTop3ByDestId(String destId) {
        List<Strategy> list = strategyRepository.findByDestIdOrderByViewnum(destId);
        List<Strategy> strategies = new ArrayList<>();
        if (list.size()>=3){
            strategies = list.subList(0, 3);
        }
        return strategies;
    }


}
